//
//  PAMobileRegisterViewController.m
//  PAMobileWallet
//
//  Created by Pay_SMP002 on 13-7-3.
//  Copyright (c) 2013年 平安付. All rights reserved.
//

#import "PAMobileRegisterViewController.h"
#import "PAMobileRegisterView.h"
#import "PublicDefine.h"
#import "PARealNameAuthViewController.h"
#import "PANavView.h"
#import "PAHeadPicViewController.h"

#import "PADataCenter.h"
#import "PANetworkManager.h"
#import "PAChekText.h"

unsigned int seconds = GET_VALID_CODE_TIMER;

@interface PAMobileRegisterViewController ()

@end

@implementation PAMobileRegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        mobileRegisterView = [[PAMobileRegisterView alloc] initWithFrame:RECT_PHONE];
        mobileRegisterView.parentController = self;
        mobileRegisterView.backgroundColor = BGVIEW_COLOR;
        self.view = mobileRegisterView;
        
        navView = [[PANavView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        navView.delegate = self;
        [navView setTitle:@"用手机号注册"];
        [mobileRegisterView addSubview:navView];
    }
    return self;
}

#pragma mark - PANavViewDelegate methods

- (void)sendSelf:(PANavView *)sendView
{
    [self.navigationController popViewControllerAnimated:NO];
    [[NSNotificationCenter defaultCenter] postNotificationName:NOT_NeedRelogin object:nil];
}

//短信验证
- (void)checkMessage:(id)sender
{
    NSMutableDictionary * postingDic = [[NSMutableDictionary alloc] initWithDictionary:@{k_operationType:@"op_apply_sms_code",k_loginId:mobileRegisterView.userNameTF.text,k_useType:[NSNumber numberWithInt:1],k_phoneNum:mobileRegisterView.userNameTF.text}];
    [[PANetworkManager sharedInstance] reqAsynParamDic:postingDic userInfo:nil delegate:self];
    
    messageTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];
}

//倒计时方法
-(void)timerFireMethod:(NSTimer *)theTimer
{
    if (seconds == 1)
    {
        [theTimer invalidate];
        seconds = GET_VALID_CODE_TIMER;
        [mobileRegisterView.checkButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setBackgroundImage:[UIImage imageNamed:@"card_check3.png"] forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setEnabled:YES];
        [mobileRegisterView.checkButton setTintColor:[UIColor darkTextColor]];
        mobileRegisterView.checkButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    }else
    {
        seconds--;
        NSString * title = [NSString stringWithFormat:MSG_DYNAMIC_CODE_WAIT,seconds];
        [mobileRegisterView.checkButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setBackgroundImage:[UIImage imageNamed:@"card_check4.png"] forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setEnabled:NO];
        mobileRegisterView.checkButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [mobileRegisterView.checkButton setTitle:title forState:UIControlStateNormal];
    }
}

- (void)releaseTImer
{
    if (messageTimer)
    {
        if ([messageTimer respondsToSelector:@selector(isValid)])
        {
            if ([messageTimer isValid])
            {
                [messageTimer invalidate];
                seconds = GET_VALID_CODE_TIMER;
            }
        }
    }
}

- (void)testNext:(id)sender
{
    NSMutableDictionary *postingDic = [[NSMutableDictionary alloc] initWithDictionary:@{k_operationType:@"op_confirm_sms_code",k_loginId:mobileRegisterView.userNameTF.text,k_useType:[NSNumber numberWithInt:1],k_messageCode:mobileRegisterView.messageCheckTF.text}];
    [[PANetworkManager sharedInstance] reqAsynParamDic:postingDic userInfo:nil delegate:self];
}

- (void)paRequestRespons:(NSDictionary *)responsDic userInfo:(NSDictionary *)userInfo
{
    NSString *op = [responsDic objectForKey:k_operationType];
    if ([op isEqualToString:@"op_register_phone_num"])
    {
        [mobileRegisterView.checkButton setBackgroundImage:[UIImage imageNamed:@"card_check3.png"] forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setEnabled:YES];
        [mobileRegisterView.checkButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setTintColor:[UIColor darkGrayColor]];
        mobileRegisterView.checkButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    }
    
    if ([op isEqualToString:@"op_apply_sms_code"])
    {
        mobileRegisterView.checkButton.enabled = NO;
        mobileRegisterView.messageCheckTF.enabled = YES;
        [mobileRegisterView.messageCheckTF becomeFirstResponder];
    }
    
    if ([op isEqualToString:@"op_confirm_sms_code"])
    {
        NSLog(@"headPicController == %@",headPicController);
        if (headPicController == nil)
        {
            headPicController = [[PAHeadPicViewController alloc] initWithNibName:nil bundle:nil];
            headPicController.parentController = self;
        }
        markType = 2;
        [headPicController sendPhoneNum:mobileRegisterView.userNameTF.text];
        [headPicController sendmarkType:1];
        
        [[NSUserDefaults standardUserDefaults] setObject:mobileRegisterView.userNameTF.text forKey:@"mobileRegisterUserName"];
        [self.navigationController pushViewController:headPicController animated:YES];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)sendmarkType:(int)getmarkType
{
    markType = getmarkType;
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    mobileRegisterView.resultButton.enabled = NO;
    [mobileRegisterView.resultButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png"] forState:UIControlStateNormal];
    mobileRegisterView.resultButton.titleLabel.textColor = ENABLE_COLOR;

    if (markType == 1)
    {
        mobileRegisterView.userNameTF.text = @"";
    }
    mobileRegisterView.userNameTF.delegate = self;
    mobileRegisterView.messageCheckTF.text = @"";
    mobileRegisterView.messageCheckTF.delegate = self;
    mobileRegisterView.messageCheckTF.enabled = NO;
    [mobileRegisterView.userNameTF becomeFirstResponder];
    
    [self releaseTImer];
    
    if ([mobileRegisterView.userNameTF.text length] == 11)
    {
        [mobileRegisterView.checkButton setBackgroundImage:[UIImage imageNamed:@"card_check3.png"] forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setEnabled:YES];
        [mobileRegisterView.checkButton setTintColor:[UIColor darkTextColor]];
    }
    else
    {
        mobileRegisterView.checkButton.selected = NO;
        [mobileRegisterView.checkButton setBackgroundImage:[UIImage imageNamed:@"card_check4.png"] forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setEnabled:NO];
        [mobileRegisterView.checkButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
    
    [mobileRegisterView.checkButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    mobileRegisterView.checkButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    

    [mobileRegisterView.userNameTF addTarget:self action:@selector(changeCheckButtonState:) forControlEvents:UIControlEventEditingChanged];
    [mobileRegisterView.messageCheckTF addTarget:self action:@selector(changeNextButtonState:) forControlEvents:UIControlEventEditingChanged];
}

- (void)changeCheckButtonState:(UITextField *)getUserNameTF
{
    //if ([mobileRegisterView.userNameTF.text length] == 11)
    if ([PAChekText validMobileNo:mobileRegisterView.userNameTF.text] == 1)
    {
        NSMutableDictionary *postingDic = [[NSMutableDictionary alloc] initWithDictionary:@{k_operationType: @"op_register_phone_num",k_mobilePhoneNum:mobileRegisterView.userNameTF.text}];
        [[PANetworkManager sharedInstance] reqAsynParamDic:postingDic userInfo:nil delegate:self];
    }
    else
    {
        seconds = GET_VALID_CODE_TIMER;
        [self releaseTImer];
        
        mobileRegisterView.checkButton.enabled = NO;
        [mobileRegisterView.checkButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setBackgroundImage:[UIImage imageNamed:@"card_check4.png"] forState:UIControlStateNormal];
        [mobileRegisterView.checkButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        mobileRegisterView.checkButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];

        mobileRegisterView.resultButton.enabled = NO;
        [mobileRegisterView.resultButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png"] forState:UIControlStateNormal];
        mobileRegisterView.resultButton.titleLabel.textColor = ENABLE_COLOR;
    }
}

- (void)changeNextButtonState:(UITextField *)getMessageTF
{
    //if ([mobileRegisterView.messageCheckTF.text length] == 6)
    if ([PAChekText validVerificationCode:mobileRegisterView.messageCheckTF.text] == 1)
    {
        mobileRegisterView.resultButton.enabled = YES;
        [mobileRegisterView.resultButton setBackgroundImage:[UIImage imageNamed:@"button_A1.png"] forState:UIControlStateNormal];
        [mobileRegisterView.resultButton setBackgroundImage:[UIImage imageNamed:@"button_A2.png"] forState:UIControlStateHighlighted];
        [mobileRegisterView.resultButton setTitleColor:ABLE_COLOR forState:UIControlStateNormal];
        [mobileRegisterView.resultButton setTitleColor:ABLE_COLOR forState:UIControlStateHighlighted];
    }
    else
    {
        mobileRegisterView.resultButton.enabled = NO;
        [mobileRegisterView.resultButton setBackgroundImage:[UIImage imageNamed:@"button_B1.png"] forState:UIControlStateNormal];
        mobileRegisterView.resultButton.titleLabel.textColor = ENABLE_COLOR;
    }
}

#pragma mark - UITextFieldDelegate  methods

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == mobileRegisterView.userNameTF) {
        mobileRegisterView.userBackImage.image = [UIImage imageNamed:@"login_account2.png"];
    }else{
        mobileRegisterView.passBackImage.image = [UIImage imageNamed:@"login_password2.png"];
    }
    [textField setTextColor:[UIColor blackColor]];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField == mobileRegisterView.userNameTF) {
        mobileRegisterView.userBackImage.image = [UIImage imageNamed:@"login_account.png"];
    }else{
        mobileRegisterView.passBackImage.image = [UIImage imageNamed:@"login_password.png"];
    }
    [textField setTextColor:[UIColor grayColor]];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
